package concurrent.TaskQueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MultiMainDemo {

    public static void main(String[] args) {
        threadPoolExecutors();
    }

    public static void TaskQueue(){
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        TaskQueue taskQueue = new TaskQueue(3);

        taskQueue.start(); // 某机构开始工作。

        for (int i = 0; i < 50; i++) {
            // new 10 个需要办事的人，并且进入某机构办事。
            PrintTask task = new PrintTask(i);
            taskQueue.add(task);
        }
        System.out.println("over");
    }

    public static void executors(){
        TaskExecutors executors = new TaskExecutors(4);
        for (int i = 0; i < 2000; i++) {
            PrintTask task = new PrintTask(i);
            System.out.println("任务数量：" + executors.add(task));
        }
        System.out.println("over");
    }

    public static void threadPoolExecutors(){
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 200, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(),new ThreadPoolExecutor.CallerRunsPolicy());
        try {
            System.out.println("start");
            for(int i=1;i<=20;i++){//执行20个任务，但第16个任务开始就会被丢弃。
                PrintTask task = new PrintTask(i);
                executor.execute(() -> task.run());
                System.out.println(executor.getQueue().size());
            }
            System.out.println("end");
        } catch (Exception e) {
            e.printStackTrace();
        }
        //该线程用于对线程池的检测
        //new Thread(new CheckTask(executor)).start();

    }
}
